package com.ymatou.iapi.sellertrading.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.AcceptOrderCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.YMTDateUtil;

public class Ts_AcceptOrder {

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单无需补款");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new AcceptOrderCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AcceptOrder_001() {
		Logger.start(true, "买手接单");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);

			//把生成的订单号更新到脚本中
			Map<String, Object> ac = new HashMap<String, Object>();
			ac.put("OrderId", orderid);

			//callandgetreturn
			JSONObject response = new AcceptOrderCall(sellerid, userid)
					.callAndGetReturnData(ac);
			//验证返回值
			Logger.verifyEquals("操作成功", response.getString("Msg"), "操作成功");
			//验证sql
			DefaultWapper df = new DefaultWapper();
			Logger.verifyEquals(17,
					df.selectOrderByOderId(orderid).get(0)
							.get("iTradingStatus"), "订单表状态更新");
			//验证买手接单时间，误差不超过5秒
			AssertService.assertTime(
					YMTDateUtil.getDate(),
					YMTDateUtil.parseYMDHMSDate(df.selectOrderByOderId(orderid)
							.get(0).get("dAcceptTime").toString()), 5);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AcceptOrder_002() {
		Logger.start(false, "非订单所有者接单时失败");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);

			//把生成的订单号更新到脚本中
			Map<String, Object> ac = new HashMap<String, Object>();
			ac.put("OrderId", orderid);

			//callandgetreturn
			int sellerid1 = sellerid + 1;//错误的买手ID
			JSONObject response = new AcceptOrderCall(sellerid1, userid)
					.callAndGetReturnData(ac);
			//验证返回值
			Logger.verifyEquals(201, response.get("Code"), "接口返回201");
			Logger.verifyEquals("用户" + sellerid1 + "不是订单" + orderid + "的所有者",
					response.get("Msg"), "失败原因");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AcceptOrder_003() {
		Logger.start(false, "订单号不存在时接单失败");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);

			//把生成的订单号更新到脚本中
			Map<String, Object> ac = new HashMap<String, Object>();
			ac.put("OrderId", 123);

			//callandgetreturn
			JSONObject response = new AcceptOrderCall(sellerid, userid)
					.callAndGetReturnData(ac);
			//验证返回值
			Logger.verifyEquals(201, response.get("Code"), "接口返回201");
			Logger.verifyEquals("订单号123不存在", response.get("Msg"), "错误原因");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AcceptOrder_004() {
		Logger.start(false, "未付款的订单不能接单");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();

			//把生成的订单号更新到脚本中
			Map<String, Object> ac = new HashMap<String, Object>();
			ac.put("OrderId", orderid);

			//callandgetreturn
			JSONObject response = new AcceptOrderCall(sellerid, userid)
					.callAndGetReturnData(ac);
			//验证返回值
			Logger.verifyEquals(201, response.get("Code"), "接口返回201");
			Logger.verifyEquals("当前订单状态不能确认接单", response.get("Msg"), "错误原因");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
